<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 添加分类按钮区域 -->
      <el-row>
        <el-col>
          <el-button type="primary" @click="showAddCateDialog"
            >添加分类</el-button
          >
        </el-col>
      </el-row>
      <!-- 分类表格  -->
      <tree-table
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        border
        :show-row-hover="false"
      >
        <template slot="isok" slot-scope="scope">
          <i
            class="el-icon-success"
            v-if="scope.row.cat_deleted === false"
            style="color: lightgreen"
          ></i>
          <i class="el-icon-error" v-else style="color: red"></i>
        </template>

        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag
            size="mini"
            type="success"
            v-else-if="scope.row.cat_level === 1"
            >二级</el-tag
          >
          <el-tag size="mini" type="warning" v-else>三级</el-tag>
        </template>

        <!-- 操作 -->
        <template slot="opt">
          <el-button size="mini" type="primary" icon="el-icon-edit"
            >编辑</el-button
          >
          <el-button size="mini" type="danger" icon="el-icon-delete"
            >删除</el-button
          >
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 点击添加分类 -->
    <el-dialog
      title="添加分类"
      :visible.sync="Showhidden"
      width="50%"
      @close="CloseWindows"
    >
      <!-- :before-close="handleClose" -->
      <!-- 添加分类表单 -->
      <el-form
        :model="addCateForm"
        :rules="addCateFormRules"
        ref="addCateFormRef"
        label-width="100px"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类" prop="cat_pid">
          <!-- expandTrigger='hover'(鼠标悬停触发级联) v-model(设置级联菜单绑定数据) :options(指定级联菜单数据源)  :props(用来配置数据显示的规则) 
  clearable(提供“X”号完成删除文本功能) change-on-select(是否可以选中任意一级的菜单) -->
          <el-cascader
            expandTrigger="hover"
            v-model="selectedKeys"
            :options="parentCateList"
            :props="cascaderProps"
            @change="parentCateChange"
            clearable
            change-on-select
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="Showhidden = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getCateList, setCateList } from "@/api/commodity";
export default {
  name: "",
  components: {},
  props: {},
  data() {
    return {
      cateList: [], // 商品分类管理的数据

      // 请求参数
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },

      total: 0,
      //先在columns中添加一个列
      columns: [
        { label: "分类名称", prop: "cat_name" },
        //type:'template'(将该列设置为模板列)，template:'isok'(设置该列模板的名称为isok)
        { label: "是否有效", prop: "", type: "template", template: "isok" },
        { label: "排序", prop: "", type: "template", template: "order" },
        { label: "操作", prop: "", type: "template", template: "opt" },
      ],

      // 弹出层显示
      Showhidden: false,
      //添加分类的表单数据对象
      addCateForm: {
        //分类名称
        cat_name: "",
        //添加分类的父级id，0则表示父级为0.添加一级分类
        cat_pid: 0,
        //添加分类的等级，0则表示添加一级分类
        cat_level: 0,
      },
      //添加分类校验规则
      addCateFormRules: {
        //验证规则
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },

      //保存1,2级父级分类的列表
      parentCateList: [],
      //配置级联菜单中数据如何展示
      cascaderProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
        expandTrigger: "hover",
      },
      //绑定用户选择的分类值
      selectedKeys: [],
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getCateList();
  },
  methods: {
    async getCateList() {
      const res = await getCateList(this.queryInfo);
      // console.log(res);
      // console.log(res  .result[0].cat_deleted);
      this.cateList = res.result;
      this.total = res.total;
    },

    load() {
      console.log("load");
    },

    handleSizeChange(newSize) {
      //当pagesize发生改变时触发
      this.queryInfo.pagesize = newSize;
      this.getCateList();
    },
    handleCurrentChange(newPage) {
      //当pagenum发生改变时触发
      this.queryInfo.pagenum = newPage;
      this.getCateList();
    },

    showAddCateDialog() {
      //调用getParentCateList获取分类列表
      this.getParentCateList();
      //显示添加分类对话框
      this.Showhidden = true;
    },
    async getParentCateList() {
      //获取父级分类数据列表
      const res = await getCateList();
      // console.log("188:", res);
      // if (res.meta.status !== 200) {
      //   return this.$message.error("获取商品分类列表数据失败");
      // }
      this.parentCateList = res;
    },

    CloseWindows() {
      // console.log(123);
      //当关闭添加分类对话框时，重置表单
      this.$refs.addCateFormRef.resetFields();
      this.selectedKeys = [];
      this.addCateForm.cat_pid = 0;
      this.addCateForm.cat_level = 0;
    },

    // 当弹出层的下拉菜单发生变化就会触发这个函数 这里用的级联菜单组件
    parentCateChange() {
      // console.log("下拉的级联菜单发生变化:", this.selectedKeys);
      if (this.selectedKeys.length > 0) {
        // 父级分类
        // console.log("");
        this.addCateForm.cat_pid =
          this.selectedKeys[this.selectedKeys.length - 1];
        this.addCateForm.cat_level = this.selectedKeys.length;
        return;
      } else {
        this.addCateForm.cat_pid = 0;
        this.addCateForm.cat_level = 0;
        return;
      }
    },

    // 添加分类
    addCate() {
      // console.log("添加分类");
      this.$refs.addCateFormRef.validate(async (isOK) => {
        if (!isOK) return;
        // console.log("248:", isOK);

        try {
          await setCateList(this.addCateForm);
          // console.log("250:", res);

          this.getCateList();
          this.$message.success("添加分类成功");
          this.Showhidden = false;
        } catch (error) {
          this.$message.error("添加失败");
          // console.log();
        }
      });
    },
  },
};
</script>

<style scoped></style>
